using Spire.Xls;using System;using System.Data;using System.Windows.Forms;namespace ExcelToImage{ public partial class Form1 : Form { private OpenFileDialog openF; DataTable dt = new DataTable(); public Form1() { InitializeComponent(); } private void Button1_Click(object sender, EventArgs e) { for(int i=0; i< dataGridView1.Rows.Count;i ) { string wjmc = dataGridView1.Rows[i].Cells["文件名称"].Value.ToString(); string wjlj = dataGridView1.Rows[i].Cells["文件路径"].Value.ToString(); try { Workbook workbook = new Workbook(); workbook.LoadFromFile(wjlj); //将第一张工作表保存为图片 Worksheet sheet = workbook.Worksheets[0]; sheet.SaveToImage(Application.StartupPath @"\ImageList\" wjmc ".jpg"); } catch (Exception) { } } dt = null; dataGridView1.DataSource = dt; System.Diagnostics.Process.Start(Application.StartupPath @"\ImageList\"); } private void Button2_Click(object sender, EventArgs e) { openF = new OpenFileDialog(); openF.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); if (openF.ShowDialog() != DialogResult.OK) { return; } DataRow dr = dt.NewRow(); dr["文件名称"] = openF.SafeFileName.Remove(openF.SafeFileName.LastIndexOf(".")); dr["文件路径"] = openF.FileName; dt.Rows.Add(dr); dataGridView1 .DataSource= dt; } private void Form1_Load(object sender, EventArgs e) { dt.Columns.Add("文件名称", typeof(string)); dt.Columns.Add("文件路径", typeof(string)); } }}
评论